<template>
  <div class="dictionaries">
    <!-- <div>22222222222</div> -->
    <!-- <div v-for="(item,index) in searchData" :key="index"> -->
    <!-- 搜索框组件 -->
    <!-- <div v-if="modelType === 'search'"></div> -->
    <!-- 查看组件 -->
    <!-- <div></div> -->
    <!-- 编辑组件 -->
    <!-- <div></div> -->
    <!-- 表格组件 -->
    <!-- <div></div> -->
    <!-- </div> -->
  </div>
</template>

<script>
import {
  // getTSType,
  getTSTypeTable,
  getTSTypePlus,
} from '@/api/dma/dictionaries/index'
export default {
  name: 'dictionaries',
  props: {
    searchData: {
      type: Array,
      required: true,
    },
    modelType: {
      type: String,
      required: true,
    },
    newData: {},
  },
  data () {
    return {
      dictionaryData: {
        dictionaryObj: {},
        // 字典分组编码
        typeGroupCode: '',
      },
      // 转换后的options数据
      optionData: [],
      // option的假数据
      option: [
        { label: '请选择', value: '0' },
        { label: '已绑定', value: '1' },
        { label: '未绑定', value: '2' },
      ],
      list: {},
    }
  },
  mounted () {
    if (this.searchData) {
      const searchDataT = JSON.stringify(this.searchData)
      if (searchDataT) {
        this.dictionaryData.dictionaryObj = JSON.parse(
          JSON.stringify(this.searchData)
        )
      }
    }
  },
  methods: {
    keyValue (beforeObj, afterObj) {
      let list = this.list
      // @ts-ignore
      beforeObj[afterObj['EN']] = list[afterObj['code']]
      return beforeObj
    },
    // 字典表查询   |    跨表查询
    // 字典表查询
    getTypeGroupCode () {
      return new Promise(async (resolve, reject) => {
        let searchData = this.searchData
        // 设置code 的数组
        let codeArr = []
        let ENArr = []
        // 设置一个空对象
        var Obj = {}
        for (let i = 0; i < searchData.length; i++) {
          // 遍历后获取字典编码
          let typeGroupCode = searchData[i].typeGroupCode
          // console.log(typeGroupCode,357);

          // 遍历后获得每一个的EN值
          if (typeGroupCode) {
            codeArr.push(typeGroupCode)
            // console.log(codeArr,357);
            let index = i
            let EN = searchData[index].EN
            let enobj = {}
            enobj.EN = EN
            enobj.code = typeGroupCode
            ENArr.push(enobj)
            // console.log(typeGroupCode,'canshu');
            // const res = await getTSType(typeGroupCode)
            // this.optionData = res.data
            // console.log(res.data,'19565sss6');
            //optionDatas  请求返回的数组
            // console.log(res,951);
            // let optionDatas = res.data
            // --------

            // --------
            // 转换value,label  ---------------
            // optionDatas.forEach(item => {
            //     for(var key in item) {
            //         if( key === 'typeCode' ) {
            //             item['value'] = item[key]
            //             delete item[key]
            //         }
            //         if( key === 'typeName') {
            //             item['label'] = item[key]
            //             delete item[key]
            //         }
            //     }
            //     Obj[EN] = optionDatas
            // })

            // searchData[i].option = optionDatas
          }
        }
        // for( var i=0;i<arr.length;i++){console.log(obj[arr[i].EN]=[])}
        const res = await getTSTypePlus(codeArr)
        let dataObj = res.data
        this.list = res.data
        // for (let key in dataObj) {
        //     for (let ind = 0; ind < ENArr.length; ind++) {
        //         if (ENArr[ind].code === key) {
        //             key = ENArr[ind].EN
        //         }

        //     }
        // }
        // for(let item of list1){
        //     console.log(item)
        // }
        // @ts-ignore
        // this.keyValue()
        const newObj = ENArr.reduce(this.keyValue, {})
        // console.log(newObj, 63635)

        // -----------------
        // const list3 = ENArr.map(item =>({
        //     [item.EN]:dataObj[item.code]
        // }))
        // console.log(list3,321);
        // console.log(res.data,364);
        // console.log(ENArr,365);
        // console.log(dataObj,366);
        resolve(newObj)
      })
      // this.sendOptionData(Obj)
    },
    // 跨表查询
    getTypeGroupCodeAcross () {
      return new Promise(async (resolve, reject) => {
        let searchData = this.searchData
        let objAcross = {}
        for (let i = 0; i < searchData.length; i++) {
          if (searchData[i].dictTable) {
            let table = searchData[i].dictTable.table
            let id = searchData[i].dictTable.id
            let name = searchData[i].dictTable.name
            let associated = searchData[i].dictTable.associated
            if (!associated) {
              associated = ''
            }
            let index = i
            let EN = searchData[index].EN
            /*   console.log(
              index,
              '----index------',
              searchData[index].EN,
              table,
              id,
              name,
              associated
            )
 */
            const res = await getTSTypeTable(table, id, name, associated)
            // console.log(res, '----每条结果------')
            // const res = await getTSTypeTable(table,id,name,associated='')
            // console.log(table,id,name,associated,'ceshiyixia sdskjk');

            searchData[i].option = optionDatas
            // console.log(res.data,'996');
            let optionDatas = res.data
            objAcross[EN] = optionDatas
            // console.log(optionDatas);
          }
          // console.log(objAcross,'getTypeGroupCodeAcross ceshi ');
        }
        resolve(objAcross)
      })
    },
    // 传递optionData
    sendOptionData (option) {
      this.$emit('optionData', option)
      // this.$emit('searchData',this.searchData)
    },
    // sendOptionDataTable(option){
    //     this.$emit('optionDataTable',option)
    // }
  },
  async created () {
    console.log(5555)
    const result = await Promise.all(
      [this.getTypeGroupCode(), this.getTypeGroupCodeAcross()].map((p) =>
        p.catch((e) => console.log(e))
      )
    )
    //const result = await this.getTypeGroupCodeAcross()
    // console.log(result, 6666);
    this.sendOptionData(result)
    // this.sendOptionData()
  },
}
</script>

<style></style>
